package com.gwcm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwcm.exception.BusinessException;
import com.gwcm.mapper.*;
import com.gwcm.model.dto.orderInfo.PosSaleOrderQueryRequest;
import com.gwcm.model.entity.*;
import com.gwcm.model.vo.PosSaleOrderVo;
import com.gwcm.model.vo.SaleItemVo;
import com.gwcm.service.OrderInfoService;
import com.gwcm.service.ProductService;
import com.gwcm.util.PageUtil;
import com.yhz.commonutil.constant.SortConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
* @author 15375
* @description 针对表【order_info】的数据库操作Service实现
* @createDate 2023-03-11 15:41:21
*/
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private SaleMapper saleMapper;
    @Resource
    private SaleItemMapper saleItemMapper;


//    /**
//     * 订单分页查询
//     * @param orderInfoQueryRequest 查询条件
//     * @return
//     * @throws BusinessException
//     */
//    @Override
//    public IPage<OrderInfoVo> pageList(OrderInfoQueryRequest orderInfoQueryRequest) throws BusinessException {
//        IPage<OrderInfo> page = PageUtil.vaildPageParam(orderInfoQueryRequest.getCurrent(), orderInfoQueryRequest.getPageSize());
//        String sortField = orderInfoQueryRequest.getSortField();
//        String sortOrder = orderInfoQueryRequest.getSortOrder();
//        String nickName = orderInfoQueryRequest.getNickName();
//        String productName = orderInfoQueryRequest.getName();
//        if (!StrUtil.isBlank(nickName)) {
//            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//            userQueryWrapper.like("nickname", nickName);
//            User user = (User) userMapper.selectOne(userQueryWrapper);
//            orderInfoQueryRequest.setNickName(user.getNickname());
//        }
//
//        if (!StrUtil.isBlank(productName)) {
//            QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
//            productQueryWrapper.like("name", productName);
//            Product product = (Product) productMapper.selectOne(productQueryWrapper);
//            orderInfoQueryRequest.setName(product.getName());
//        }
//
//        if (StrUtil.isBlank(sortOrder) || !StrUtil.equalsAny(sortOrder, SortConstant.SORT_ORDER_ASC, SortConstant.SORT_ORDER_DESC)) {
//            //默认按升序排序
//            sortOrder = SortConstant.SORT_ORDER_ASC;
//        }
//        orderInfoQueryRequest.setSortField(sortField);
//        orderInfoQueryRequest.setSortOrder(sortOrder);
//        System.out.println(orderInfoQueryRequest);
//        IPage<OrderInfoVo> orderInfoToIPage=orderInfoMapper.selectOrderInfoToPage(page, orderInfoQueryRequest);
//        IPage<OrderInfoVo> orderInfoVoIPage = orderInfoToIPage.convert(item -> {
//            OrderInfoVo orderInfoVo = new OrderInfoVo();
//            BeanUtils.copyProperties(item, orderInfoVo);
//            return orderInfoVo;
//        });
//
//        return orderInfoVoIPage;
//    }

    /**
     * 收银销售订单分页查询
     * @param posSaleOrderQueryRequest
     * @return
     * @throws BusinessException
     */
    @Override
    public IPage<PosSaleOrderVo> pageList(PosSaleOrderQueryRequest posSaleOrderQueryRequest) throws BusinessException {
        IPage<Sale> page = PageUtil.vaildPageParam(posSaleOrderQueryRequest.getCurrent(), posSaleOrderQueryRequest.getPageSize());
        String sortField = posSaleOrderQueryRequest.getSortField();
        String sortOrder = posSaleOrderQueryRequest.getSortOrder();

        if (StrUtil.isBlank(sortOrder) || !StrUtil.equalsAny(sortOrder, SortConstant.SORT_ORDER_ASC, SortConstant.SORT_ORDER_DESC)) {
            //默认按升序排序
            sortOrder = SortConstant.SORT_ORDER_ASC;
        }
        posSaleOrderQueryRequest.setSortField(sortField);
        posSaleOrderQueryRequest.setSortOrder(sortOrder);
        IPage<PosSaleOrderVo> posSaleOrderToIPage = orderInfoMapper.selectPosSaleOrderToPage(page, posSaleOrderQueryRequest);
        IPage<PosSaleOrderVo> posSaleOrderVoIPage = posSaleOrderToIPage.convert(item -> {
            PosSaleOrderVo posSaleOrderVo = new PosSaleOrderVo();
            BeanUtils.copyProperties(item, posSaleOrderVo);
            System.out.println("我是posSaleOrderVo.getSaleId()："+posSaleOrderVo.getSaleId());

            List<SaleItemVo> saleItemVoList = selectSaleItemBySaleId(posSaleOrderVo.getSaleId());
            posSaleOrderVo.setSaleItemVoList(saleItemVoList);
            return posSaleOrderVo;
        });

        return posSaleOrderVoIPage;
    }

//    /**
//     * 通过saleId获得销售明细
//     * @param saleId
//     * @return
//     */
//    public List<List<SaleItemVo>> selectSaleItemBySaleId(Long saleId){
//        //通过saleId查到saleItem（可能多条）
//        SaleItem params = new SaleItem();
//        params.setSaleId(saleId);
//        List<SaleItem> saleItemList = saleItemMapper.selectSaleItemList(params);
//        System.out.println("我是saleItemList："+saleItemList);
//        //通过saleItem表的productId查到productName
//
//        //调用saleItemMapper获得saleItemList
//        List<List<SaleItemVo>> saleItemVoList1 = new ArrayList<>();
//        for (SaleItem si : saleItemList){
//            SaleItemVo saleItemVo = new SaleItemVo();
//            List<SaleItemVo> saleItemVoList2 = new ArrayList<>();
//            Product product;
//
//            product = productMapper.selectProductById(si.getProductId());
//            //把值存到saleItemVo
//
//            saleItemVo.setProductName(product.getName());
//            saleItemVo.setItemSn(product.getProductSn());
//            saleItemVo.setQuantity(si.getQuantity());
//            saleItemVo.setPrice(si.getPrice());
//
//            saleItemVoList2.add(saleItemVo);
//            saleItemVoList1.add(saleItemVoList2);
//            System.out.println(saleItemVoList1);
//        }
//        //返回
//        return saleItemVoList1;
//    }

    /**
     * 通过saleId获得销售明细
     * @param saleId
     * @return
     */
    public List<SaleItemVo> selectSaleItemBySaleId(Long saleId){
        //通过saleId查到saleItem（可能多条）
        SaleItem params = new SaleItem();
        params.setSaleId(saleId);
        List<SaleItem> saleItemList = saleItemMapper.selectSaleItemList(params);
        //通过saleItem表的productId查到productName

        //调用saleItemMapper获得saleItemList
        List<SaleItemVo> saleItemVoList = new ArrayList<>();
        Product product;
        for (SaleItem si : saleItemList){
            SaleItemVo saleItemVo = new SaleItemVo();
            product = productMapper.selectProductById(si.getProductId());
            //把值存到saleItemVo
            saleItemVo.setProductName(product.getName());
            saleItemVo.setItemSn(product.getProductSn());
            saleItemVo.setQuantity(si.getQuantity());
            saleItemVo.setPrice(si.getPrice());
            saleItemVoList.add(saleItemVo);
        }
        //返回
        return saleItemVoList;
    }
}




